﻿@{
    Layout = "~/Views/_LayoutNull.cshtml";
}



<form class="form-inline">
    <div class="form-group">
        <div class="input-group">
            <div class="input-group-addon">商品分类:</div>
            @Html.DropDownList("CategoryProductId", ViewData["CategoryProduct"] as SelectList, htmlAttributes: new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        <button type="button" class="btn btn-primary">新增商品</button>
        </div>
</form>
 
<div id="tableDetail">
    <table class="table">
        <thead>
            <tr>
                <th>编码</th>
                <th>条形码</th>
                <th>名称</th>
                <th>品牌</th>
                <th>型号</th>
                <th>单位</th>
                <th>价格</th>
                <th><button type="button" class="btn btn-warning">全选</button></th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
</div>

<script type="text/javascript">
    $(document).ready(function () {
        //新增商品
        $(".btn-primary").click(function () {
            window.open("/Basic/ProductNew");
        });

        $(".btn-warning").click(function () { 
            $(".btn-danger").each(function () {
                var v =$(this).parent().parent().children("td:first").text();
                window.opener.GetProduct(v);
            });
        });
        
        function AddTableTr(trHtml) {
            var $tr = $("#tableDetail tr:first");
            if ($tr.size() == 0) {
                alert("指定的table id或行数不存在！");
                return;
            }
            $tr.after(trHtml);
        }

        function RemoveTableTr() {
            var row = $("#tableDetail").find("tr").length;
            while (row > 1) {
                var $tr = $("#tableDetail tr").eq(1);
                if ($tr.size() == 0) {
                    alert("指定的table id或行数不存在！");
                    return;
                }
                $tr.remove();
                row = $("#tableDetail").find("tr").length;
            };
        }

        $("body").on("click", ".btn-danger", function () {
            var v = $(this).parent().parent().children("td:first").text();
            window.opener.GetProduct(v);
        });

        $("#CategoryProductId").change(function () {
            RemoveTableTr();
            $.get("/Basic/GetProductListJson", {
                categoryId: $("#CategoryProductId").val(),
                type:@ViewData["ProductType"]
            }, function (data) {
                var ot = '@ViewData["OrderType"]';
                $.each(data, function (i, item) {
                    var pri = "";
                    if (ot == "j") {
                        pri = item["PurchasePrice"];
                    }
                    else if (ot == "x") {
                        pri = item["SalePrice"];
                    }
                    else {
                        pri = item["StorePrice"];
                    }

                    var tr = '<tr><td>' + item["Code"] + '</td>';
                    tr += ('<td>' + item["Barcode"] + "</td>");
                    tr += ('<td>' + item["Name"] + "</td>");
                    tr += ('<td>' + item["BrandName"] + "</td>");
                    tr += ('<td>' + item["ModelName"] + "</td>");
                    tr += ('<td>' + item["UnitName"] + "</td>");
                    tr += ('<td>' +pri + '</td>');
                    tr += ('<td><button type="button" class="btn btn-danger">选择</button></td></tr>');
                    AddTableTr(tr);
                });
            });
        });

        $("#CategoryProductId").change();
    });
</script>


